package org.cxa.cableModemMonitor;

import org.jsoup.nodes.Element;


/**
 * Copyright 2014 opencxa.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Christopher Armenio
 *
 */
public abstract class ChannelReading
{
	public enum ChannelStatus
	{
		STATUS_LOCKED("Locked"),
		STATUS_NOT_LOCKED("Not Locked");
		
		private final String statStr;
		
		private ChannelStatus(String statStringIn) { this.statStr = statStringIn; }
		
		
		public static ChannelStatus fromString(String statusStringIn)
		{
			for( ChannelStatus currStatus : ChannelStatus.values() )
			{
				if( currStatus.statStr.equals(statusStringIn) ) return currStatus;
			}
			return null;
		}
		
		
		public String toString() { return this.statStr; }
	}
	
	
	protected final int chanNum;
	protected final ChannelStatus chanStatus;
	protected final int chanId;
	protected final float frequency;
	protected final float power;
	
	
	protected ChannelReading(int expectedChanNumIn, Element chanNumElemIn, Element chanStatusElemIn, Element chanIdElementIn, Element frequencyElemIn, Element powerElemIn)
	{	
		this.chanNum = Integer.parseInt(chanNumElemIn.text()); 
		if( this.chanNum != expectedChanNumIn ) throw new IllegalArgumentException(
				String.format("mismatched channel numbers - got %d, expected %d", this.chanNum, expectedChanNumIn));
		
		this.chanStatus = ChannelStatus.fromString(chanStatusElemIn.text());
		if( this.chanStatus == null ) throw new IllegalArgumentException(
				String.format("invalid channelStatus - %s", chanStatusElemIn.text()));
		
		this.chanId = Integer.parseInt(chanIdElementIn.text());
		
		String freqStrings[] = frequencyElemIn.text().split(" ");
		if( (freqStrings.length != 2) || !freqStrings[1].equals("Hz") ) throw new IllegalArgumentException(
				String.format("invalid frequency string - %s", frequencyElemIn.child(4).text()));
		this.frequency = Float.parseFloat(freqStrings[0]);
		
		String powerStrings[] = powerElemIn.text().split(" ");
		if( (powerStrings.length != 2) || !powerStrings[1].equals("dBmV") ) throw new IllegalArgumentException(
				String.format("invalid power string - %s", powerElemIn.child(5).text()));
		this.power = Float.parseFloat(powerStrings[0]);
	}

	
	/**
	 * @return the chanNum
	 */
	public int getChanNum()
	{
		return chanNum;
	}


	/**
	 * @return the chanStatus
	 */
	public ChannelStatus getChanStatus()
	{
		return chanStatus;
	}


	/**
	 * @return the chanId
	 */
	public int getChanId()
	{
		return chanId;
	}


	/**
	 * @return the frequency
	 */
	public float getFrequency()
	{
		return frequency;
	}


	/**
	 * @return the power
	 */
	public float getPower()
	{
		return power;
	}
	
	
	public abstract String getCsvString();
}
